package com.jcl.recruit.user.logic;

import com.jcl.recruit.core.common.page.AbstractEntityPage;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.user.mapper.UserMapper;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.vo.UserSearchEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/14 9:19
 * @Version 1.0
 **/
@Component
public class UserLogic extends AbstractEntityPage<UserSearchEntity> implements UserMapper {

    @Autowired
    private UserMapper userMapper;

    public Map<Integer, User> getUserMap(String corpCode, List<Integer> ids){
        List<User> userList = userMapper.findUserListByIds(corpCode, ids);
        Map<Integer, User> userMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(userList)){
            for(User user: userList){
                userMap.put(user.getId(), user) ;
            }
        }
        return userMap;
    }

    public int countMembers(String corpCode) {
        Integer numbers = 0;

        Map<String, Object> map =  userMapper.countMember(corpCode);
        if(null != map &&  null != map.get("members")){
            numbers = Integer.parseInt(map.get("members").toString());
        }

        return numbers;
    }

    public Map<String, User> getAdminUserByCorpCode(List<String> corpCodes){
        Map<String, User> userMap = new HashMap<>();
        List<User> userList = userMapper.findAdminUserListByCorp(corpCodes);
        if(CollectionUtils.isNotEmpty(userList)){
            for(User user: userList){
                userMap.put(user.getCorpCode(), user);
            }
        }
        return userMap;
    }
    public Map<String, User> getCorpCodeUserByUserId(List<Integer> userIds){
        Map<String, User> userMap = new HashMap<>();
        List<User> userList = userMapper.findUserList(userIds);
        if(CollectionUtils.isNotEmpty(userList)){
            for(User user: userList){
                userMap.put(user.getCorpCode(), user);
            }
        }
        return userMap;
    }
    @Override
    protected List<?> searchConditionList(UserSearchEntity entity) {
        if(UserSearchEntity.TYPE_CERTIFY.equals(entity.getType())){
            entity.setStatus(Constants.UserInfo.USER_STATUS_UNACTIVE);
            return userMapper.findListByEntity(entity);
        }else if(UserSearchEntity.TYPE_ALL.equals(entity.getType())){
            return userMapper.findListByEntity(entity);
        }
        return null;
    }

    @Override
    public List<Map<String,Object>> selectUsersByCondition(String realname, Integer ishr, Integer subcompanyId, Integer departmentId) {
        List<Map<String,Object>> userVos = userMapper.selectUsersByCondition(realname,ishr,subcompanyId,departmentId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Map<String,Object> entity:userVos){
            entity.put("createtime",entity.get("createtime"));
        }
        return userVos;
    }

    @Override
    public List<User> findListByEntity(UserSearchEntity entity) {
        return userMapper.findListByEntity(entity);
    }

    @Override
    public List<User> findAll(String corpCode) {
        return userMapper.findAll(corpCode);
    }

    public List<User> findListCertification(String corpCode) {
        return userMapper.findListByStatus(corpCode, Constants.UserInfo.USER_STATUS_UNACTIVE);
    }

    @Override
    public List<User> findUserListByIds(String corpCode, List<Integer> ids) {
        return userMapper.findUserListByIds(corpCode, ids);
    }

    @Override
    public List<User> findUserList(List<Integer> ids) {
        return userMapper.findUserList(ids);
    }

    @Override
    public List<User> findListByStatus(String corpCode, Integer status) {
        return userMapper.findListByStatus(corpCode, status);
    }

    @Override
    public List<User> findAdminUserListByCorp(List<String> corpCodes) {
        return userMapper.findAdminUserListByCorp(corpCodes);
    }

    @Override
    public Map<String,Object> selectPrimaryKey(String username, String password) {

        return userMapper.selectPrimaryKey(username,password);
    }
    @Override
    public Map<String, Object> selectMaxIdKey() {
        return userMapper.selectMaxIdKey();
    }

    @Override
    public Map<String, Object> countMember(String corpCode) {
        return userMapper.countMember(corpCode);
    }

    @Override
    public Map<String,Object> selectPrimaryKeyByToken(String token) {
        return userMapper.selectPrimaryKeyByToken(token);
    }
    @Override
    public Map<String,Object> selectPrimaryKeyById(Integer id) {
        return userMapper.selectPrimaryKeyById(id);
    }

    @Override
    public Map<String,Object> selectPrimayByUsername(String username) {
        return userMapper.selectPrimayByUsername(username);
    }

    @Override
    public User findOneById(Integer id) {
        return userMapper.findOneById(id);
    }

    @Override
    public User findAdminUserByCorp(String corpCode) {
        return userMapper.findAdminUserByCorp(corpCode);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateUserHead(Integer id, String photoId) {
        return userMapper.updateUserHead(id, photoId);
    }

    @Override
    public Integer selectPrimaryIdByEmail(String email) {
        return userMapper.selectPrimaryIdByEmail(email);
    }
}
